<?php
	$this->breadcrumbs=array(
		'Books'=>array('index'),
		'Upload',
	);
 ?>
 
<br/>
<h1>Upload Book</h1>
<br/>
<?php echo $message;?>
<script type = 'text/javascript'>
	function validate() {
		var warning = document.getElementById('ebookWarning');
		var file = document.forms['uploadForm']['file[]'][0].value;
		if (file == null || file == '' || file.length < 4) {
			warning.innerHTML = 'please select a file';
			return false;
		} else if (file.substring(file.length - 4, file.length).toLowerCase() != '.pdf') {
			warning.innerHTML = 'accept only pdf format';
			return false;
		} else {
			file = document.forms['uploadForm']['file[]'][1].value;
			if (file != null && file != '' && file.length >= 4) {
				if (
					file.substring(file.length - 4, file.length).toLowerCase() == '.jpg'
					|| file.substring(file.length - 4, file.length).toLowerCase() == '.gif'
				) {
					return true;
				} else {
					warning = document.getElementById('imageWarning');
					warning.innerHTML = 'accept only jpg format';
					return false;
				}
			} else {
				return true;
			}
		}
	}
</script>

<div class = 'form'>
	<form
		name = 'uploadForm'
		action = '?r=Upload/upload'
		method = 'POST'
		enctype = 'multipart/form-data'
		onsubmit = "return validate()"
	>
		<table>
			<tr>
				<td align = 'RIGHT'>Author:</td>
				<td>
					<input type = 'TEXT' name = 'author' />
				</td>
			</tr>
			<tr>
				<td align = 'RIGHT'>Ebook:</td>
				<td>
					<input type = 'FILE' name = 'file[]' />
					<div id = 'ebookWarning' style = 'color:red'></div>
				</td>
			</tr>
			<tr>
				<td align = 'RIGHT'>Book cover:</td>
				<td>
					<input type = 'FILE' name = 'file[]' />
					<div id = 'imageWarning' style = 'color:red'></div>
				</td>
			</tr>
			<tr>
				<td colspan = 2>Description:</td>
			</tr>
			<tr>
				<td colspan = 2>
					<textarea rows = 4 cols = 60 name = 'description'>your description</textarea>
				</td>
			</tr>
			<tr>
				<td colspan = 2>Genre:</td>
			</tr>
			<tr>
				<td>
					<?php
						$genreList;
						foreach(Genre::model()->findAll() as $genre) {
							$genreList[$genre->idgenre] = $genre->name;
						}
						echo CHtml::checkBoxList(
							'genreList',
							'',
							$genreList
						);
					?>
				</td>
			</tr>
			<tr>
				<td colspan = 2 align = 'CENTER'>
					<input
						type = 'SUBMIT'
						value = 'Upload'
					/>
				</td>
			</tr>
		</table>
	</form>
</div>